package com.ruoyi.common.utils;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.security.Md5Util;

import java.util.*;


/**
 * <p>Title: RandomUtils</p>
 * <p>Description: TODO 随机数生成</p>
 *
 * @author 小短腿跑路贼溜
 * @date 2021年5月28日
 */
public class RandomUtils {


    /**
     * <p>Title: generateCode</p>
     * <p>Description: 生成6位随机码   如B5H447,AKNQ70</p>
     *
     * @return 返回6位随机码
     */
    public static String generateCode(int size) {

        String charList = "0123456789";
        String numList = "0123456789";
        String rev = "";
        // 数字最多不超过四位
        int maxNumCount = 4;
        // 6位邀请码，字母数字混合
        int length = size;
        Random f = new Random();
        for (int i = 0; i < length; i++) {
            if (f.nextBoolean() && maxNumCount > 0) {
                maxNumCount--;
                rev += numList.charAt(f.nextInt(numList.length()));
            } else {
                rev += charList.charAt(f.nextInt(charList.length()));
            }
        }
        return rev;
    }

    public static String generateCodeABC(int size) {

        String charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String numList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String rev = "";
        // 数字最多不超过四位
        int maxNumCount = 4;
        // 6位邀请码，字母数字混合
        int length = size;
        Random f = new Random();
        for (int i = 0; i < length; i++) {
            if (f.nextBoolean() && maxNumCount > 0) {
                maxNumCount--;
                rev += numList.charAt(f.nextInt(numList.length()));
            } else {
                rev += charList.charAt(f.nextInt(charList.length()));
            }
        }
        return rev;
    }

    public static String doSort(String pwd){
        //将字符串拆分成字符数组
        char[] chars = pwd.toCharArray();
        List<Character> characters = new ArrayList<>();
        //将字符存入到集合中
        for (char aChar : chars) {
            characters.add(aChar);
        }
        //随机乱序排列
        String ordernessPassword = characters.stream()
                //拉姆达表达式,随机返回排序结果
                .sorted((o1, o2) -> new Integer[]{-1, 0, 1}[new Random().nextInt(3)])
                //拉姆达表达式,拼接回字符串
                .map(c -> c + "")
                .reduce((v1, v2) -> v1 + v2)
                .get();
        return ordernessPassword;
    }


    public static void main(String[] args) {
//测试字符串
        String pwd = "123456abc";
        //将字符串拆分成字符数组
        char[] chars = pwd.toCharArray();
        List<Character> characters = new ArrayList<>();
        //将字符存入到集合中
        for (char aChar : chars) {
            characters.add(aChar);
        }
        //随机乱序排列
        String ordernessPassword = characters.stream()
                //拉姆达表达式,随机返回排序结果
                .sorted((o1, o2) -> new Integer[]{-1, 0, 1}[new Random().nextInt(3)])
                //拉姆达表达式,拼接回字符串
                .map(c -> c + "")
                .reduce((v1, v2) -> v1 + v2)
                .get();
        System.out.println(ordernessPassword);

    }
}

